define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/yueorder/index' + location.search,
                    add_url: 'shop/yueorder/add',
                    edit_url: 'shop/yueorder/edit',
                    del_url: 'shop/yueorder/del',
                    multi_url: 'shop/yueorder/multi',
                    import_url: 'shop/yueorder/import',
                    table: 'yue_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                singleSelect:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'amount', title: __('金额'), operate: false},
                        {field: 'order_no', title: __('订单编号'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user.mobile', title: __('用户')},
                        // {field: 'yue_id', title: __('Yue_id')},
                        {
                            field: 'yueorder.status',
                            title: __('Status'),
                            searchList: {"refund": __('已退款'), "payed": __('已支付')},
                            formatter: function (value, row, index) {
                                console.log(row.status);
                                var statusVal = row.status;
                                if (statusVal === 'payed') {
                                    return '<span style="color:red">已支付</span>';
                                } else if (statusVal === 'refund') {
                                    return '<span style="color:grey">已退款</span>';
                                } else if (statusVal === 'paying') {
                                    return '待支付';
                                } else {
                                    return '其他';
                                }
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('下单时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'list',
                                    title: __('退款'),
                                    text: '退款',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/yueorder/refund?ids=" + row.id;
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refund: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
